package cn.itcast.cd.action;

import org.apache.struts2.ServletActionContext;

import sun.org.mozilla.javascript.internal.ContextAction;
import cn.itcast.cd.domain.User;
import cn.itcast.cd.query.IQuery;
import cn.itcast.cd.query.Query;
import cn.itcast.cd.service.ILoginService;
import cn.itcast.cd.service.IUserService;
import cn.itcast.cd.utils.SessionUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final String LOGIN = "login";
	private String userName;
	private String password;
	private ILoginService loginService;
	

	public void setLoginService(ILoginService loginService) {
		this.loginService = loginService;
	}

	@Override
	public String execute() throws Exception {
		
		//System.out.println(userName+","+password);
		//LOGIN登陆没有被过检查权限和检查登陆拦截器过滤,如果直接访问 ,就没有用户名和密码.
		if(userName!=null){
			User user = loginService.getUser(userName, password);
			//System.out.println(user);
			if(user!=null){
				//将登陆的用记放进SESSION中.
				SessionUtils.setUserInSession(user);
				return SUCCESS;			
			}
			//如果验证失败,就返回提示信息.
			//System.out.println("兄弟你至少要给我返回个东西噻/");
			ActionContext.getContext().put("errorMsg", "用户名或者密码错误,请重新输入.");	
		}
		
		//注意一个问题,将项目从新配置到TOMCAT后,USERNAME会改变.
		//ActionContext.getContext().put("errorMsg", "用户名为null.");	
		return "login";
	}
	
	

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}
}
